package com.infocollect.project.api.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.infocollect.common.utils.ServletUtils;
import com.infocollect.framework.security.LoginUser;
import com.infocollect.framework.security.service.TokenService;
import com.infocollect.project.api.entity.TappComControlEntity;
import com.infocollect.project.api.helper.CurrentHelp;
import com.infocollect.project.api.mapper.TappComControlMapper;
import com.infocollect.project.api.req.ComControlPage;
import com.infocollect.project.api.service.ITappComControlService;
import com.infocollect.project.api.vo.CurrentUserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 刘亚平
 * @since 2021-06-23
 */
@Service
public class TappComControlServiceImpl extends ServiceImpl<TappComControlMapper, TappComControlEntity> implements ITappComControlService {
    @Autowired
    private TokenService tokenService;
    /**
     * 通过主键获取数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public TappComControlEntity getById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 通过主键删除数据
     *
     * @param fid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long fid) {
        return this.baseMapper.deleteById(fid) > 0;
    }

    @Override
    public boolean addComControlInfo(TappComControlEntity req) {
//        CurrentUserInfoVO currentUserInfo = CurrentHelp.Ins.GetUser();
        LoginUser currentUserInfo = tokenService.getLoginUser(ServletUtils.getRequest());
        ///req.setFid(IdWorker.getId());
        req.setFcreateby(currentUserInfo.getUserName());
        req.setFlastupdateby(currentUserInfo.getUserName());
        req.setFcreationdate(new Date());
        req.setFlastupdatedate(new Date());
        this.baseMapper.insert(req);
        return true;
    }

    /**
     * 查询用户控件列表
     * @param page
     * @return
     */
    @Override
    public List<TappComControlEntity> queryComControlList(ComControlPage page){
        LambdaQueryWrapper<TappComControlEntity> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(page.getUserControlName())) {
            lambdaQueryWrapper.like(TappComControlEntity::getControlname, page.getUserControlName());
        }
        if(StringUtils.isNotBlank(page.getControlType())) {
            lambdaQueryWrapper.eq(TappComControlEntity::getControlType, page.getControlType());
        }

        return baseMapper.selectList(lambdaQueryWrapper);

    }
}

